// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.rcp.intro.actions;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.text.Collator;
import java.util.Properties;

import org.dengues.commons.utils.FileUtils;
import org.dengues.rcp.i18n.Messages;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.IWorkbenchWindow;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-6-27 qiang.zhang $
 * 
 */
public class AboutDenguesAction extends Action {

    /**
     * Qiang.Zhang.Adolf@gmail.com AboutDenguesAction class global comment. Detailled comment <br/>
     * 
     * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-6-27 qiang.zhang $
     * 
     */
    private class AboutDialog extends Dialog {

        /**
         * Qiang.Zhang.Adolf@gmail.com AboutDialog constructor comment.
         * 
         * @param parentShell
         */
        protected AboutDialog(Shell parentShell) {
            super(parentShell);
            setShellStyle(getShellStyle() | SWT.RESIZE);
        }

        /*
         * (non-Javadoc)
         * 
         * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
         */
        @Override
        protected void configureShell(Shell newShell) {
            super.configureShell(newShell);
            newShell.setText(Messages.getString("AboutDialog.Title"));
            newShell.setSize(400, 300);
        }

        /*
         * (non-Javadoc)
         * 
         * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
         */
        protected void createButtonsForButtonBar(Composite parent) {
            createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
        }

        /*
         * (non-Javadoc)
         * 
         * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
         */
        @Override
        protected Control createDialogArea(Composite parent) {
            // top level composite
            Composite parentComposite = (Composite) super.createDialogArea(parent);

            parentComposite.setLayout(new FillLayout());

            TabFolder tabFolder = new TabFolder(parentComposite, SWT.NULL);

            TabItem tabItem1 = new TabItem(tabFolder, SWT.NULL);
            tabItem1.setText(Messages.getString("AboutDialog.Tab.About"));

            TabItem tabItem2 = new TabItem(tabFolder, SWT.NULL);
            tabItem2.setText(Messages.getString("AboutDialog.Tab.Credits"));

            TabItem tabItem3 = new TabItem(tabFolder, SWT.NULL);
            tabItem3.setText(Messages.getString("AboutDialog.Tab.License"));

            TabItem tabItem4 = new TabItem(tabFolder, SWT.NULL);
            tabItem4.setText(Messages.getString("AboutDialog.Tab.ConfigInfo"));

            newAboutItem(tabItem1, tabFolder);
            newCreditsItem(tabItem2, tabFolder);
            newLicenseItem(tabItem3, tabFolder);
            newSystemProperties(tabItem4, tabFolder);

            return parentComposite;
        }

    }

    /**
     * Qiang.Zhang.Adolf@gmail.com AboutDenguesAction class global comment. Detailled comment <br/>
     * 
     * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-6-27 qiang.zhang $
     * 
     */
    private class LProvider extends LabelProvider implements ITableLabelProvider {

        public Image getColumnImage(Object arg0, int arg1) {
            return null;
        }

        public String getColumnText(Object element, int columnIndex) {
            java.util.Map.Entry cp = (java.util.Map.Entry) element;
            if (columnIndex == 0)
                return cp.getKey().toString();
            else
                return cp.getValue().toString();
        }
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com AboutDenguesAction class global comment. Detailled comment <br/>
     * 
     * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-6-27 qiang.zhang $
     * 
     */
    private class MyViewerSorter extends ViewerSorter {

        public MyViewerSorter() {
            super();
        }

        public MyViewerSorter(Collator collator) {
            super(collator);
        }

        public boolean isSorterProperty(Object element, String propertyId) {
            return true;
        }
    }

    private IWorkbenchWindow window;

    /**
     * Qiang.Zhang.Adolf@gmail.com AboutDenguesAction constructor comment.
     */
    public AboutDenguesAction(IWorkbenchWindow window) {
        this.window = window;
        // setId("id_about_dengues");
        // setActionDefinitionId("id_about_dengues");
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "newSystemProperties".
     * 
     * @param tabItem4
     * @param tabFolder
     */
    public void newSystemProperties(TabItem itemTab, TabFolder parent) {
        final Properties props = System.getProperties();

        TableViewer tv = new TableViewer(parent, SWT.NULL);
        tv.setSorter(new MyViewerSorter());
        Table table = tv.getTable();
        TableColumn c1 = new TableColumn(table, SWT.NULL);
        c1.setText(Messages.getString("Property_9")); //$NON-NLS-1$
        TableColumn c2 = new TableColumn(table, SWT.NULL);
        c2.setText(Messages.getString("Value_10")); //$NON-NLS-1$
        table.setLinesVisible(true);
        table.setHeaderVisible(true);

        TableLayout tableLayout = new TableLayout();
        for (int i = 0; i < 2; i++)
            tableLayout.addColumnData(new ColumnWeightData(1, 50, true));
        table.setLayout(tableLayout);

        itemTab.setControl(tv.getControl());
        tv.setContentProvider(new IStructuredContentProvider() {

            public Object[] getElements(Object input) {
                return props.entrySet().toArray();
            }

            public void dispose() {
            }

            public void inputChanged(Viewer viewer, Object arg1, Object arg2) {
            }
        });
        tv.setLabelProvider(new LProvider());
        tv.setInput(this);

    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "newLicenseItem".
     * 
     * @param tabItem3
     * @param tabFolder
     */
    public void newLicenseItem(TabItem item, TabFolder parent) {
        Composite cmp = new Composite(parent, SWT.NULL);
        item.setControl(cmp);
        GridLayout lay = new GridLayout();
        lay.numColumns = 1;
        cmp.setLayout(lay);

        StyledText st = new StyledText(cmp, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
        st.setEditable(false);
        String separator = System.getProperty("line.separator"); //$NON-NLS-1$
        BufferedReader bbr = null;
        try {
            File licFile = FileUtils.getFileFromBundle("org.dengues.libraries", "DENGUES_LICENSE");
            bbr = new BufferedReader(new FileReader(licFile));
            String str;
            StringBuffer all = new StringBuffer();
            while ((str = bbr.readLine()) != null) {
                all.append(str);
                all.append(separator);
            }
            st.setText(all.toString());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (bbr != null)
                    bbr.close();
            } catch (java.io.IOException e) {
            }
        }
        GridData data = new GridData(GridData.FILL_BOTH | GridData.VERTICAL_ALIGN_CENTER | GridData.CENTER);
        st.setLayoutData(data);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "newCreditsItem".
     * 
     * @param tabItem2
     * @param tabFolder
     */
    public void newCreditsItem(TabItem item, TabFolder parent) {
        Composite cmp = new Composite(parent, SWT.NULL);
        item.setControl(cmp);
        GridLayout lay = new GridLayout();
        lay.numColumns = 1;
        cmp.setLayout(lay);

        StyledText st = new StyledText(cmp, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
        st.setEditable(false);
        String separator = System.getProperty("line.separator"); //$NON-NLS-1$
        String credits = "";
        credits += "Developers (versions 1.0.0):" + separator + " - Qiang.Zhang.Adolf (qiang.zhang.adolf@gmail.com)" + separator;
        credits += "Developers (versions 0.1.4):" + separator + " - Qiang.Zhang.Adolf (qiang.zhang.adolf@gmail.com)" + separator;
        credits += "Developers (versions 0.1.3):" + separator + " - Qiang.Zhang.Adolf (qiang.zhang.adolf@gmail.com)" + separator;
        credits += "Dengues Studio uses the following libraries too:" + separator;
        credits += " - Eclipse (http://www.eclipse.org/) Eclipse Development Platform" + separator;
        credits += " - log4j (http://jakarta.apache.org/log4j) Logging API" + separator;
        credits += " - dom4j (http://www.dom4j.org)  Dom4j API" + separator;

        st.setText(credits);
        GridData data = new GridData(GridData.FILL_BOTH | GridData.VERTICAL_ALIGN_CENTER | GridData.CENTER);
        st.setLayoutData(data);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "newAboutItem".
     * 
     * @param tabItem1
     * @param tabFolder
     */
    public void newAboutItem(TabItem item, TabFolder parent) {
        Composite cmp = new Composite(parent, SWT.NULL);
        item.setControl(cmp);
        GridLayout lay = new GridLayout();
        lay.numColumns = 1;
        lay.marginWidth = 15;
        lay.marginHeight = 15;
        cmp.setLayout(lay);

        Label lb = new Label(cmp, SWT.NULL);
        lb.setText(Messages.getString("AboutDialog.About.copyright"));

        GridData data = new GridData(GridData.VERTICAL_ALIGN_CENTER | GridData.CENTER);
        lb.setLayoutData(data);
        lb.setSize(SWT.DEFAULT, 50);

    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.action.Action#run()
     */
    @Override
    public void run() {
        AboutDialog aboutDialog = new AboutDialog(this.window.getShell());
        aboutDialog.open();
    }
}
